﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hk.Core.Extensions;
using Hk.Core.General;

namespace Hk.Core
{
    public class Post : BaseDbContext
    {
        public tbl_Blog_Posts GetByAlias(string alias)
        {
            SetHit(1, alias);
            return _db.tbl_Blog_Posts.FirstOrDefault(a => a.post_name.Equals(alias) && a.post_status == "publish" && a.portal_id == CurrentPortal.PortalId);
        }

        public List<CustomPost> GetPostByCategory(int currentPage, int itemPerPage, string alias)
        {
            var cacheKey = "GetPostByCategory_" + alias;
            // Check the cache
            var posts = CacheLayer.Get<List<CustomPost>>(cacheKey);
            if (posts == null)
            {
                var query = "PaginationPost " + currentPage + ", " + itemPerPage + ", 'id', '" + alias + "', " + CurrentPortal.PortalId;
                posts = _db.Database.SqlQuery<CustomPost>(query).ToList();
                CacheLayer.Add(posts, cacheKey);
            }
            return posts;
        }

        public List<CustomPost> GetAllPostHomePage(int currentPage, int itemPerPage)
        {
            var cacheKey = "GetAllPostHomePage" + currentPage;
            // Check the cache
            var posts = CacheLayer.Get<List<CustomPost>>(cacheKey);
            if (posts == null)
            {
                posts = _db.Database.SqlQuery<CustomPost>("PaginationPost " + currentPage + ", " + itemPerPage + ", 'id', '', " + CurrentPortal.PortalId).ToList();
                CacheLayer.Add(posts, cacheKey);
            }
            return posts;
        }

        public string GetPaginationHomePage(int currentPage, int pageSize, int itemPerPage, string query, string url)
        {
            var cacheKey = "GetPaginationHomePage" + currentPage;
            // Check the cache
            var pagination = CacheLayer.Get<string>(cacheKey);
            if (pagination == null)
            {
                pagination = _db.Database.SqlQuery<string>("Pagination " + currentPage + ", " + pageSize + ", " + itemPerPage + ", '" + query + "', " +
                                                           "'" + url + "', " + CurrentPortal.PortalId + ", ''").FirstOrDefault();
                CacheLayer.Add(pagination, cacheKey);
            }
            return pagination;
        }

        public string GetPaginationByCategory(int currentPage, int pageSize, int itemPerPage, string query, string url, string alias)
        {
            var cacheKey = "GetPaginationByCategory" + alias;
            // Check the cache
            var pagination = CacheLayer.Get<string>(cacheKey);
            if (pagination == null)
            {
                pagination = _db.Database.SqlQuery<string>("Pagination " + currentPage + ", " + pageSize + ", " + itemPerPage + ", '" + query + "', " +
                                                           "'" + url + "', " + CurrentPortal.PortalId + ", '" + alias + "'").FirstOrDefault();
                CacheLayer.Add(pagination, cacheKey);
            }
            return pagination;
        }

        public List<vGetAllPost> GetTopView(int top)
        {
            var cacheKey = "GetTopView" + top;
            // Check the cache
            var posts = CacheLayer.Get<List<vGetAllPost>>(cacheKey);
            if (posts == null)
            {
                posts = _db.vGetAllPosts.Where(a => a.PortalID == CurrentPortal.PortalId)
                    .OrderByDescending(a => a.Hit).Take(top).ToList();
                CacheLayer.Add(posts, cacheKey);
            }
            return posts;
        }

        public void SetHit(int number, string alias)
        {
            var obj = _db.tbl_Blog_Posts.FirstOrDefault(a => a.post_name.Equals(alias) && a.post_status == "publish" && a.portal_id == CurrentPortal.PortalId);
            if (obj != null)
            {
                if (obj.view_count != null)
                    obj.view_count += number;
                else
                    obj.view_count = 1;
            }
            _db.SaveChanges();
        }
    }

    public partial class tbl_Blog_Posts : BaseDbContext
    {
        public string MetaTitle
        {
            get
            {
                var postMeta = _db.Database.SqlQuery<string>("select top 1 meta_value from tbl_Blog_Postmeta where post_id = " + ID + " and meta_key = '" + PostMeta.meta_title + "'").FirstOrDefault();
                return !string.IsNullOrEmpty(postMeta) ? postMeta : post_title;
            }
        }

        public string MetaKeyword
        {
            get
            {
                var postMeta = _db.Database.SqlQuery<string>("select top 1 meta_value from tbl_Blog_Postmeta where post_id = " + ID + " and meta_key = '" + PostMeta.meta_keyword + "'").FirstOrDefault();
                return !string.IsNullOrEmpty(postMeta) ? postMeta : "";
            }
        }

        public string MetaDescription
        {
            get
            {
                var postMeta = _db.Database.SqlQuery<string>("select top 1 meta_value from tbl_Blog_Postmeta where post_id = " + ID + " and meta_key = '" + PostMeta.meta_description + "'").FirstOrDefault();
                return !string.IsNullOrEmpty(postMeta) ? postMeta : post_excerpt;
            }
        }
    }
}
